/*
 * notificationDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package com.popsGUI.java;

/**
 *
 * @author  __USER__
 */
public class notificationDialog extends javax.swing.JDialog {

	/** Creates new form notificationDialog */
	public notificationDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		nd_text = new javax.swing.JLabel();
		nd_Ok_Button = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		nd_text.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

		nd_Ok_Button.setText("OK");
		nd_Ok_Button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				nd_Ok_ButtonActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				171,
																				171,
																				171)
																		.add(
																				nd_Ok_Button))
														.add(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.add(
																				nd_text,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				376,
																				Short.MAX_VALUE)))
										.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().add(29, 29, 29).add(nd_text,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 203,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(18,
						18, 18).add(nd_Ok_Button).addContainerGap(25,
						Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void nd_Ok_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
		this.setEnabled(false);
		this.setVisible(false);
		this.nd_text.setText("");
		this.nd_Ok_Button.setEnabled(false);
	}

	public void displayNotification(String str) {
		//format string
		String finalStr = "";
		int WindowCharWidth = 50;
		String begin = str.substring(0, 6);
		System.out.println("str.lenght: " + str.length() + " ::" + str);
		if (begin.equals("<html>")) {
			finalStr = str;
		} else if (str.length() < WindowCharWidth) {
			System.out.println("passed");
			finalStr = str;
		} else {
			System.out.println("formatting:");
			finalStr = finalStr.concat("<html><p>");
			int processed = 0;
			boolean flag = true;

			while (flag) {
				//if the remainder will fit don't bother
				int end_index = processed + WindowCharWidth;
				if (end_index > str.length()) {
					flag = false;
					end_index = str.length();
				} else //find last space before
				{
					int fromIndex = processed;
					int spaceIndex = -1;
					System.out.println("starting loop; str:" + str);
					while (fromIndex < end_index) {
						spaceIndex = str.indexOf(" ", fromIndex);
						fromIndex = fromIndex + 5;
						System.out.println("SpaceIndex = " + spaceIndex
								+ "fromIndex = " + fromIndex);
					}

					if (spaceIndex == -1 || spaceIndex <= processed) {
						// keep the end index the same?
					} else {
						end_index = fromIndex;
					}
				}

				finalStr += str.substring(processed, end_index);

				finalStr += "<p>";
				processed = end_index;
				System.out.println(processed + " : " + end_index + "::"
						+ finalStr);
			}

			finalStr += "</html></p>";
			System.out.println(finalStr);

		}

		//display string
		this.setEnabled(true);
		this.setVisible(true);
		this.nd_Ok_Button.setEnabled(true);
		this.nd_text.setText(finalStr);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				notificationDialog dialog = new notificationDialog(
						new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton nd_Ok_Button;
	private javax.swing.JLabel nd_text;
	// End of variables declaration//GEN-END:variables

}